package com.example.controller;

import com.example.entity.*;
import com.example.service.UserService;
import com.sky.result.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.yaml.snakeyaml.events.Event;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Api
@RestController
@CrossOrigin(origins = "http://localhost:5173")
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService service;

    @PostMapping("/Recording")
    @ApiOperation("根据match查询信息")
    public Result<List<Recording>> getBymatchCriteria(@RequestBody matchCriteria matchcriteria) {
        System.out.println(matchcriteria.toString());
        List<Recording> recording=service.getRecording(matchcriteria);
        return Result.success(recording);
    }
    
    @PostMapping("/orders")
    @ApiOperation("返回订单信息")
    public Result<List<Order>> test() {
        List<Order> orders=service.orders();
        return Result.success(orders);
    }

    @PostMapping("/insertv")
    @ApiOperation("增加重车过磅信息")
    public Result<Long> insertV(@RequestBody vehicle_weight_record vehicleWeightRecordecord) {
        Long id=service.insertV(vehicleWeightRecordecord);
        System.out.println(id);
        return Result.success(id);
    }

    @PostMapping("/insertn")
    @ApiOperation("增加空车过磅信息")
    public Result<returnNull> insertN(@RequestBody insertNull insertnull) {
        System.out.println(insertnull);
        Long id=service.SearchV(insertnull.getLicensePlate(),insertnull.getHeavyweighingTime());
        nullWeightRecord record=new nullWeightRecord();
        BeanUtils.copyProperties(insertnull,record);
        record.setGrossWeightId(id);
        Date date = new Date();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        record.setWeighingTime(formatter.get2DigitYearStart());
        System.out.println("record"+record);
        Long id_null=service.insertN(record);


        Long id_order=service.VSearchOrder(id);
        System.out.println("id_order"+id_order);
        Order order=service.getOneOrder(id_order);

        String remark=service.VSearchremark(id);
        returnNull returnnull=new returnNull(order,insertnull.getLicensePlate(),insertnull.getWeightHeavy(),insertnull.getWeightHeavy()-insertnull.getWeight(),insertnull.getWorkType(),remark);
        System.out.println("returnnull"+returnnull);

        service.insertReceived_weight(id_order,insertnull.getWeightHeavy()-insertnull.getWeight());

        orderRecord  orderRecord=new orderRecord(insertnull.getLicensePlate(),order.getOrderName(),insertnull.getWorkType(),order.getSupplier(),order.getMaterialName(),order.getMaterialSpecification(),order.getMaterialUnits(),remark,insertnull.getWeightHeavy()-insertnull.getWeight(),insertnull.getWeightHeavy(),order.getMaterialShould());
        service.insertOrder_details(orderRecord);
        return Result.success(returnnull);
    }

    @PostMapping("/invalid")
    @ApiOperation("作废信息")
    public Result invalid(@RequestParam("id") Long id) {
        System.out.println("invalid"+id);
        service.invalid(id);
        return Result.success();
    }

    @PostMapping("/message")
    @ApiOperation("根据match查询信息")
    public String massage() {

        return "hello";
    }



//    @Resource
//    private UserService userService;
//
//    // 新增用户
//    @PostMapping
//    public Result add(@RequestBody User user) {
//        userService.save(user);
//        return Result.success();
//    }
//
//    // 修改用户
//    @PutMapping
//    public Result update(@RequestBody User user) {
//        userService.save(user);
//        return Result.success();
//    }
//
//    // 删除用户
//    @DeleteMapping("/{id}")
//    public void delete(@PathVariable("id") Long id) {
//        userService.delete(id);
//    }
//
//    // 根据id查询用户
//    @GetMapping("/{id}")
//    public Result<User> findById(@PathVariable Long id) {
//        return Result.success(userService.findById(id));
//    }
//
//    // 查询所有用户
//    @GetMapping
//    public Result<List<User>> findAll() {
//        return Result.success(userService.findAll());
//    }
//
//    // 分页查询用户
//    @GetMapping("/page")
//    public Result<Page<User>> findPage(@RequestParam(defaultValue = "1") Integer pageNum,
//                                       @RequestParam(defaultValue = "10") Integer pageSize,
//                                       @RequestParam(required = false) String name) {
//        return Result.success(userService.findPage(pageNum, pageSize, name));
//    }

}
